extends Item

var utils
var skPool
var buff
var upgraded = false
var itemType = sys.ItemType.EQUIP
func attInit():
	if utils == null:initVars()
	att = utils.loadScript("AzurAtt")	

func init():
	.init()
	initVars()
	attInit()
	setItemType()
	yield(sys.get_tree().create_timer(0.1), "timeout")
	delayInit()

func delayInit():
	pass

func initVars():
	utils = globalData.infoDs["g_azurlaneUtils"]
	skPool = globalData.infoDs["g_azurlaneItemSkills"]
	buff = globalData.infoDs["g_azurlaneBuffs"]
	type = config.EQUITYPE_EQUI

func delSelf():
	yield(sys.get_tree().create_timer(0.2), "timeout")
	sys.main.player.delItem(self)

func delFromCha():
	yield(sys.get_tree().create_timer(0.2), "timeout")
	masCha.delItem(self)

func setItemType():
	pass

func learnSkill():
	return null
	
func onDelCha():
	pass

func _connect():
	if masCha.team == 2:return
	upgradeCheck()

func upgradeCheck():
	if id.find("_ex_") > -1 or itemType != sys.ItemType.EQUIP or not has_method("upgrade"):return
	var index = 3
	if id.find("rare") > -1:
		index = 2

	if masCha.isSumm or upgraded:return
	yield(sys.get_tree().create_timer(0.1), "timeout")
	var its = []
	for i in masCha.items:
		if i.id == id and not i.upgraded and i != self:
			its.append(i)
	if its.size() == index - 1:
		upgraded = true
		call("upgrade")
		for i in its:
			i.delSelf()

func toJson():
	var json = {}
	json["upgraded"] = upgraded
	json["att"] = att.info
	json["id"] = id
	return json

func toJsonMin():
	var json = {}
	json["upgraded"] = upgraded
	json["id"] = id
	return json

func fromJson(json):
	#还原属性
	if json.get("upgraded") != null:
		set("upgraded", json["upgraded"])
	if upgraded:
		call("upgrade")
	if json.has("att"):
		var attInfo = json.get("att")
		for i in attInfo.keys():
			att.set(i, attInfo.get(i))	

func fromJsonMin(json):
	if json.get("upgraded") != null:
		set("upgraded", json["upgraded"])
	if upgraded:
		call("upgrade")